/**
 * 通过后台返回的数据选择小区信息
 * Created by Administrator on 2017/5/8.
 */

//用于只选择小区不选择楼栋
function separateSelectComm(isSelect, success,keep) {
    postJson("/comm/findCommList.x",null, function (data) {
        try {
            if (data.ret_code > 0) {
                if(keep != true) {
                    $('#selectSeparateComm').empty();
                }
                var list = data.data;
                if (list.length == 1) {
                    $("#selectSeparateComm").append('<option value="' + list[0].commCode + '">' + list[0].commName + '</option>');
                } else {
                    if (!isSelect) {
                        $("#selectSeparateComm").append('<option value="">请选择小区</option>');
                    }
                    for (var i in list) {
                        $("#selectSeparateComm").append('<option value="' + list[i].commCode + '" data-commname="'+list[i].commName+'">【' +list[i].commCode+'】'+ list[i].commName + '</option>');
                    }
                }
                layui.form().render('select');
                if (success) {
                    success();
                }
            } else {
                console.log("error: " + data.data);
            }
        } catch (err) {
            throw err;
        }
    });
}


function setSelectComm(isSelect, success,keep,onchange) {
    var form = layui.form();
    postJson("/comm/findCommList.x",null, function (data) {

        try {
            if (data.ret_code > 0) {
                var list = data.data;
                if (list.length == 1) {
                    $("#selectComm").empty().append('<option value="' + list[0].commCode + '" data-commname="'+list[0].commName+'">' + list[0].commName + '</option>');
                    setSelectBuilding(list[0].commCode);
                } else {
                    if(keep != true) {
                        $("#selectComm").empty();
                    }
                    if (!isSelect) {
                        $("#selectComm").append('<option value="">请选择小区</option>');
                    }
                    for (var i in list) {
                        $("#selectComm").append('<option value="' + list[i].commCode + '" data-commname="'+list[i].commName+'">【' +list[i].commCode+'】'+ list[i].commName + '</option>');
                    }
                    if (isSelect) {
                        setSelectBuilding(list[0].commCode);
                    }
                }
                form.render('select');
                form.on('select(selectComm)', function (data) {
                    $("#selectBuilding").empty().append('<option value="">请选择楼栋</option>');
                    setSelectBuilding(data.value);
                    if(onchange)
                        onchange(data.value);
                });
                if (success) {
                    success();
                }
            } else {
                console.log("error: " + data.data);
            }
        } catch (err) {
            throw err;
        }
    });
}

var url = "/BuildingsCtrl/getBuidingsInfoProcessed.x"; //获取楼栋信息的url

//选择楼栋
function setSelectBuilding(commCode) {
    var form = layui.form();
    var layer = layui.layer;
    form.render('select');
    var params = {
        commCode: commCode
    };

    postJson(
        url, //服务器要接受的url
        $.param(params),//传递的参数
        function (datas) { //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
            var data = datas.data;
            var list = data.buildings;
            var aliasList = data.buildingsAliasName;
            try {
                if (!isEmpty(list)) {
                    for (var i in list) {
                        $("#selectBuilding").append('<option value="' + list[i] + '">' + aliasList[i] + '</option>');
                    }
                    form.render('select');
                    form.on('select(selectBuilding)', function (data) {
                        $("#selectUnit").empty().append('<option value="">请选择单元</option>');
                        setSelectUnit(commCode, data.value);
                    });
                }
            } catch (err) {
                throw err;
            } finally {
                layer.closeAll('loading');
            }
        }
    );
}

//选择单元
function setSelectUnit(commCode, buildingCode) {
    var form = layui.form();
    var layer = layui.layer;

    var params = {
        commCode: commCode,
        buildingCode: buildingCode
    };

    postJson(
        url, //服务器要接受的url
        $.param(params),//传递的参数
        function (datas) { //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
            var data = datas.data;
            var list = data.units;
            var unitAlias = data.unitAlias;
            try {
                if (!isEmpty(list)) {
                    for (var i in list) {
                        if(unitAlias[i]!=undefined && unitAlias[i]!='')
                            $("#selectUnit").append('<option value="' + list[i] + '">' + unitAlias[i] + '</option>');
                        else
                            $("#selectUnit").append('<option value="' + list[i] + '">' + list[i] + '单元</option>');
                    }
                    form.render('select');
                    form.on('select(selectUnit)', function (data) {
                        $("#selectFloor").empty().append('<option value="">请选择楼层</option>');
                        setSelectFloor(commCode, buildingCode, data.value);
                    });
                }
            } catch (err) {
                throw err;
            } finally {
                layer.closeAll('loading');
            }
        }
    );
}

//选择楼层
function setSelectFloor(commCode, buildingCode, unitCode) {
    var form = layui.form();
    var layer = layui.layer;

    var params = {
        commCode: commCode,
        buildingCode: buildingCode,
        unitCode: unitCode
    };

    postJson(
        url, //服务器要接受的url
        $.param(params),//传递的参数
        function (datas) { //服务器返回后执行的函数 参数 data保存的就是服务器发送到客户端的数据
            var data = datas.data;
            var map = data.floors;
            try {
                if (!isEmpty(map)) {
                    //排序
                    var keys = [];
                    $.each(map, function (key, val) {
                        keys[keys.length] = key;
                    });
                    keys.sort();
                    $.each(keys, function (i, key) {
                        $("#selectFloor").append('<option value="' + key + '">' + key + '楼</option>');
                    });
                    form.render('select');
                    form.on('select(selectFloor)', function (data) {
                        $("#selectRoom").empty();
                        setSelectRoom(map[data.value]);
                    });
                }
            } catch (err) {
                throw err;
            } finally {
                layer.closeAll('loading');
            }
        }
    );
}

//选择房号
function setSelectRoom(list) {
    var form = layui.form();
    $('#selectRoom').empty();
    for (var i in list) {
        $("#selectRoom").append('<option value="' + list[i] + '">' + list[i] + '房</option>');
    }
    form.render('select');
}

function isEmpty(variable1) {
    return variable1 == null || variable1 == undefined || variable1 == '';
}

//列表选择小区
var commCodes = [];
function initCommList(pageSize, isDefaultComm) {
    var form = layui.form();
    commCodes.splice(0, commCodes.length);//清空数组
    //监听checkbox
    form.on('checkbox(selectComm)', function (data) {
        var commCode = data.value;
        if (data.elem.checked == true) {
            commCodes.push(commCode);
        } else {
            var j = -1;
            for (var i in commCodes) {
                if (commCodes[i] == commCode) {
                    j = i;
                    break;
                }
            }
            if (j != -1) {
                commCodes.splice(j, 1);
            }
        }
    });
    queryCommList(pageSize, form, isDefaultComm);
}

function queryCommList(pageSize, form, isDefaultComm) {
    var param = {};
    param.pageSize = pageSize;
    param.page = 1;
    postJson("/comm/findCommListPage.x", $.param(param), function (data) {
        $('#commBody').empty();
        render(data.list, param.page, form, isDefaultComm);
        layui.laypage({
            cont: 'commPage'
            , pages: Math.ceil(data.size / pageSize)
            , jump: function (row, first) {
                if (!first) {
                    param.page = row.curr;
                    queryCommPage(param, form, isDefaultComm);
                }
            }
        });
    });
}

function render(list, page, form, isDefaultComm) {
    $('#commBody').append();
    for (var i in list) {
        var checked = "";
        for (var j in commCodes) {
            if (list[i].comm_code == commCodes[j]) {
                checked = "checked=checked";
                break;
            }
        }
        $('#commBody').append(
            '<tr>' +
            '<td>' + list[i].comm_code + '</td>' +
            '<td>' + list[i].comm_name + '</td>' +
            '<td><input type="checkbox" name="selectComm"  lay-filter="selectComm" ' + checked + ' value="' + list[i].comm_code + '"></td>' +
            '</tr>'
        );
    }
    form.render();
}

function queryCommPage(param, form, isDefaultComm) {
    postJson("/comm/findCommListPage.x", $.param(param),
        function (data) {
            $('#commBody').empty();
            render(data.list, param.page, form, isDefaultComm);
        });
}

/*
    通过传入的Id自动生成一个layui的小区选择器,并回调外部的onChange方法
 */
function genCommSelect(id,onChange) {
    var form = layui.form();
    postJson("/comm/findCommList.x", "",
        function (data) {
            $('#'+id).empty();
            var selectStr = "";
            selectStr += '<div class="layui-form-item">' +
                            '<label class="layui-form-label">小区 :</label>' +
                            '<div class="layui-inline"> ';

            if(data.data.length >= 1){
                selectStr += "<select lay-filter='commSelect' id='aCommSelect'>";
                for(var i=0; i<data.data.length; i++){
                    selectStr += ("<option data-gatenum='"+data.data[i].gateDevNum+"' data-gatealias='"+data.data[i].gateDevAliasName+"' value='"+data.data[i].commCode+"' >"+data.data[i].commCode + "-" + data.data[i].commName+"</option>");
                }
                selectStr += "</select>";
            }

            selectStr += "</div></div>";
            $("#"+id).append(selectStr);
            form.render();

            if(onChange != null){
                onChange({elem:$("#aCommSelect"),value:$("#aCommSelect option:selected").val()});
                form.on("select(commSelect)",onChange);
            }
        });
}